#include "gamewindow.h"
#include "ui_gamewindow.h"
#include "QMessageBox"
#include "QEvent"
#include "QDebug"
#include "QPushButton"
#include "QColor"
#include "QKeyEvent"

const int a=320;
const int b=590;
const int c=860;
const int d=1130;

gamewindow::gamewindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::gamewindow)
{
    ui->setupUi(this);

    musicplayer=new QMediaPlayer;
    musicplayer->setMedia(QUrl("qrc:/opera.mp3"));
    musicplayer->setVolume(70);
    //bgm

        // 初始化音符数据
            noteDataList.append({
{0,a,0},{200, b,0},{400, c,0},{600,d,0},{800, a,0},{1400, b,0},{2000, c,0},{2200,d,0},{2600,c,0},{3000, b,0},{3400,a,0},//前奏第一句
{5600,a,0},{5800, b,0},{6000, c,0},{6200,d,0},{6400, a,0},{7000, b,0},{7600, c,0},{7800,d,0},{8200,c,0},{8600, b,0},{9000,a,0},//第二句
 {9400,d,0},{9800,c,0},{10200, b,0},{10600,a,0},
 {11000,a,0},{11200,b,0},{11400,c,0},{11600,d,0},{11800, a,0},{12400, b,0},{13000, c,0},{13200,d,0},{13600,c,0},{14000, b,0},
 {14600, b,0},{14800,a,0},{15200, b,0},{15600, c,0}, //第三句
 {16800,d,0},{17000,c,0},{17200, b,0},{17400,a,0},{17600,b,0},{18200,d,0},{18800,a,0},{19000,b,0},{19600,c,0},
 {20000,a,0},{20200,b,0},{20400,d,0},{20600,b,0},{20800,c,0},{21000,a,0},{21200,d,0},{21400,b,0},
 {21500,c,0},{21700,a,0},{21900,d,0},{22100,b,0},{22300,c,0},{22500,a,0},{22700,d,0},{22900,b,0},//前奏结束
 {23000,d,0}, {23800,c,0}, {24200,b,0}, {25000,b,0}, {25400,a,0}, {25800,b,0}, {26200,c,0},//嘲笑谁恃美扬威
 {28800,d,0}, {29600,c,0}, {30000,b,0}, {30600,b,0}, {31000,a,0}, {31400,b,0}, {31800,c,0}, //没了心如何相配
                                });


        // 启动定时器
        gameTimer = new QTimer(this);
        connect(gameTimer, &QTimer::timeout, this, &gamewindow::updateGame);


}

gamewindow::~gamewindow()
{
    delete ui;
}


void gamewindow::on_pushButton_2_clicked()
{
    startGame();

    musicplayer->play();
}//开始

void gamewindow::on_pushButton_clicked()
{
   musicplayer->stop();
}//暂停

void gamewindow::startGame() {
    qDebug()<<"start";
     gameTimer->start(16); // 16ms 间隔，约 60 FPS
     updateGame();
    // 游戏启动逻辑
}

void gamewindow::updateGame() {
    updateGameLogic(); // 更新游戏逻辑
    update(); // 触发重绘
}

void gamewindow::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    // 只绘制音符
    drawNotes(&painter);
}

void gamewindow::keyPressEvent(QKeyEvent *event) {
    int key = event->key(); // 获取按下的按键

    // 遍历音符，检查是否匹配按键
    for (auto it = notes.begin(); it != notes.end(); ) {
        if (key == Qt::Key_A && (it->x == a&&it->y>=900&&it->y<=1100)) { // 按下 A 键对应 x=100 的音符
            it = notes.erase(it); // 移除音符
        } else if (key == Qt::Key_S && (it->x == b&&it->y>=900&&it->y<=1100)) { // 按下 S 键对应 x=200 的音符
            it = notes.erase(it); // 移除音符
        } else {
            if (key == Qt::Key_K && (it->x == c&&it->y>=900&&it->y<=1100)) {
                        it = notes.erase(it); // 移除音符
                    }
            else{
                if (key == Qt::Key_L &&(it->x == d&&it->y>=900&&it->y<=1100)) {
                            it = notes.erase(it); // 移除音符
                        }
                else
                   ++it;
            }
        }
    }

    update(); // 触发重绘
}

void gamewindow::updateGameLogic() {
    qint64 currentTime = musicplayer->position(); // 获取当前播放时间

    // 遍历音符数据，生成音符
    for (auto &noteData : noteDataList) {
        if (noteData.time <= currentTime && !noteData.generated) {
            notes.append({noteData.x, 0, 70, 20,QColor(110, 120, 112, 255)}); // 生成音符
            noteData.generated = true; // 标记为已生成
        }
    }

    // 更新音符位置
    for (auto &note : notes) {
        note.y += 10; // 让音符下落
    }
}

void gamewindow::drawNotes(QPainter *painter) {
      // 创建线性渐变
        QLinearGradient gradient(0, 0, 0, height());
        gradient.setColorAt(0, Qt::white);   // 起始颜色
        gradient.setColorAt(1, QColor(149, 142, 108, 255));  // 结束颜色
        // 使用渐变填充背景
        painter->fillRect(rect(), gradient);

    for (const auto &note : notes) {
        QBrush noteBrush(note.color);
        painter->setBrush(noteBrush);
        QRect noteRect(note.x, note.y, note.width, note.height);
        painter->drawRect(noteRect);
    }
}

